package com.kgc.activityprovider.service.impl;

import com.kgc.activiti.util.ResponseMsg;
import com.kgc.activityprovider.dao.CwFkWorkFlowMapper;
import com.kgc.activityprovider.feign.cg.CgddOrderFeign;
import com.kgc.activityprovider.feign.cg.CgthOrderFeign;
import com.kgc.activityprovider.feign.cw.WorkBuyPayFeign;
import com.kgc.activityprovider.feign.cw.WorkPrePayFeign;
import com.kgc.activityprovider.feign.cw.WorkRefundFeign;
import com.kgc.activityprovider.service.WorkFlowUtilService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

@Service("cwRefundServiceImpl")
public class CwRefundServiceImpl implements WorkFlowUtilService {
    private Logger logger = LoggerFactory.getLogger(CwRefundServiceImpl.class);

    @Resource
    private CwFkWorkFlowMapper cwFkWorkFlowMapper;

    @Resource
    private WorkRefundFeign workRefundFeign;

    @Resource
    private WorkBuyPayFeign workBuyPayFeign;
    @Resource
    private WorkPrePayFeign workPrePayFeign;
    @Resource
    private CgddOrderFeign cgddOrderFeign;
    @Resource
    private CgthOrderFeign cgthOrderFeign;


    private final String TABLE = "cw_fk_refund";

    @Override
    public ResponseMsg changeStatus(String code, int status) {
        logger.info("changeStatus:code{},status{}", code,status);
        ResponseMsg responseMsg = new ResponseMsg();
        responseMsg.setCode("201");
        Map<String , Object> map = new HashMap<String , Object>();
        map.put("status", status);
        map.put("code", code);
        map.put("table", TABLE);
        int result = cwFkWorkFlowMapper.checkOrderStatus(map);
        if (result <= 0){
            responseMsg.setMsg("更新失败");
            return responseMsg;
        }
        if (status == 3){
            finishRefund(code);
        }
        responseMsg.setCode("200");
        logger.info("changeStatus:code{},status{} ,responseMsg{}" , code,status ,responseMsg);
        return responseMsg;
    }

    @Override
    public ResponseMsg insertCheck(String code, Integer checkManId, String checkComment) {
        logger.info("insertCheck code{},checkManId{},checkComment{}", code,checkManId,checkComment);
        ResponseMsg responseMsg = new ResponseMsg();
        responseMsg.setCode("201");
        Map<String,Object> map =new HashMap<>();
        map.put("checkManId", checkManId);
        map.put("checkComment", checkComment);
        map.put("code", code);
        map.put("table", TABLE);
        int result = cwFkWorkFlowMapper.insertCheckCwOtherIncome(map);
        if (result <= 0){
            return responseMsg;
        }
        responseMsg.setCode("200");
        logger.info("insertCheck code{},checkManId{},checkComment{},responseMsg{}", code,checkManId,checkComment,responseMsg);
        return responseMsg;
    }
    boolean finishRefund(String code){
        logger.info("finishRefund code{}",code);
        Map<String , Object> params = new HashMap<>();
        params.put("code", code);

        Map<String , Object> map = (Map<String, Object>)   workRefundFeign.getRefund(params).getData();
        int type = (int) map.get("sourceId");
        double amount = (double) map.get("totalAmount");
        Object sourceCode = map.get("sourceCode");
        params.clear();

        Map<String,Object> tempMap = new HashMap<>();
        switch (type){
            case 1:
                params.put("code",sourceCode);
                tempMap = (Map<String, Object>) workPrePayFeign.getPrePay(params).getData();
                Object cgddCode = tempMap.get("sourceSheet");
                params.put("code", cgddCode);
                params.put("adv_amount", amount * -1);
                cgddOrderFeign.updateCGDDData(params);
                break;
            case 2:
                break;
            case 3:
                break;
            case 4:
                tempMap.put("code",sourceCode);
                tempMap.put("finish_amount", amount);
                cgthOrderFeign.updateCGTH(tempMap);
                break;
        }
        return true;
    }
}
